﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="recordMessage.aspx.cs"
    Inherits="SampleVoiceForum.recordMessage" %>

<%@ Assembly Name="System.ServiceProcess, Version=2.0.50727.42, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %>
<%@ Import Namespace="System.ServiceProcess" %>
<%@ Import Namespace="DatabaseLayer" %>
<?xml version="1.0" ?>
<vxml version="2.0">
<catch event="connection.disconnect.hangup">
    <submit next="CleanUp.aspx"/>
    <exit/>
</catch>
<form>
<field>

<%  
    try
    {
        DatabaseFunctions objectDatabaseFunctions = new DatabaseFunctions();
        objectDatabaseFunctions.addAnalytics((String)Session["sessionId"], (String)Session["callerId"], "recordMessage.aspx", "Entered recordMessage.aspx", null);
     
        var configuration = objectDatabaseFunctions.getIVRJunctionConfiguration();
        String programFolder = configuration.Item2;

    

        // sql error
        if (configuration != null && configuration.Item1 != null)
        {
            String branchId = configuration.Item1;
            bool isModerationOnline = false;

            var generalConfigurationInformationTuple = configuration.Item6;
            isModerationOnline = generalConfigurationInformationTuple.Item1;

            if (!System.IO.Directory.Exists("C:\\inetpub\\wwwroot\\Uploads"))
            {
                System.IO.DirectoryInfo directory = System.IO.Directory.CreateDirectory("C:\\inetpub\\wwwroot\\Uploads");
            }
            HttpPostedFile file = Request.Files.Get(0);

            String audioFileName = file.FileName.Substring(file.FileName.IndexOf('-') + 1);


            file.SaveAs("C:\\inetpub\\wwwroot\\Uploads\\" + audioFileName);


            String recordingFileLocation = "C:\\inetpub\\wwwroot\\Uploads\\" + audioFileName;
            String recordingURL = "http://localhost/uploads/" + audioFileName;
            String callerId = (String)Session["callerId"];
            
            int statusOfCommand = objectDatabaseFunctions.saveRecordedData(recordingFileLocation, recordingURL, callerId, branchId, 0, false, true, null, 0, 0);

            if (statusOfCommand != 1)
            {
                // could not save the file
                Response.Write("<prompt baregin='false' timeout='1ms'>Sorry, Recording cannot be saved. Please try again later.</prompt>");
                // delete the file saved on hard disk
                System.IO.File.Delete("C:\\inetpub\\wwwroot\\Uploads\\" + file.FileName);

            }
            else
            {
                // file is saved
                objectDatabaseFunctions.addAnalytics((String)Session["sessionId"], (String)Session["callerId"], "recordMessage.aspx", "Saved recording", recordingURL);
                Response.Write("<prompt baregin='false' timeout='1ms'>Recording saved Successfully! Thank you for recording your message using Sample Voice Forum. </prompt>");
                // If Moderation is selected as online, the following code invokes the service to let it know that a new audio recording is made.
                if (isModerationOnline)
                {
                    ServiceController controlYouTubeUploadService = new ServiceController("IVR Junction- YouTube Upload Service");
                    try
                    {
                        if (controlYouTubeUploadService.Status == ServiceControllerStatus.Stopped)
                        {

                            try
                            {
                                controlYouTubeUploadService.Start();
                            }
                            catch (System.ComponentModel.Win32Exception win32Exception)
                            {
                                // log this error message
                                Console.WriteLine(win32Exception.Message);
                            }
                            catch (System.InvalidOperationException invalidOperationException)
                            {
                                // log this error message
                                Console.WriteLine(invalidOperationException.Message);
                            }
                            catch (System.ArgumentNullException argumentNullException)
                            {
                                // log this exception
                                Console.WriteLine(argumentNullException.Message);
                            }

                        }

                        try
                        {
                            controlYouTubeUploadService.ExecuteCommand(129);
                        }
                        catch (System.ComponentModel.Win32Exception win32Exception)
                        {
                            // log this exception
                            Console.WriteLine(win32Exception.Message);
                        }
                        catch (System.InvalidOperationException invalidOperationException)
                        {
                            // log this exception
                            Console.WriteLine(invalidOperationException.Message);
                        }
                    }
                    catch (Exception generalException)
                    {
                        // log this exception
                        Console.WriteLine(generalException.Message);
                    }
                }
                 // if moderation is offline then modify the prompts! 
                else
                {
                    AmplifySoundFile(recordingFileLocation, programFolder);
                }
            }

        }
        else
        {
            %>
            <prompt bargein='false' timeout='1ms'> Sorry, Recording cannot be saved. Please try again later. </prompt>
            <%
        }
    }
    catch (Exception errorMessage)
    {
        // log this error message in the event logger
        Console.WriteLine(errorMessage.Message);
        %>
        <prompt baregin='false' timeout='1ms'>
         Sorry, Recording cannot be saved. Please try again later.
        </prompt>
        <%
    }
 %>
<grammar type="text/gsl"> [poppaoomowmow] </grammar>

    <nomatch>
        <goto next="Home.aspx"/>
    </nomatch>
    
    <noinput>
        <goto next="Home.aspx" />   
    </noinput>
    <filled> 
        <goto next="Home.aspx" />   
    </filled>
     
</field>
</form>
</vxml>


 